# 测试用的Makefile
CXX = g++
CXXFLAGS = -std=c++14 -Wall -Wextra -O2 -I../src -I../build
PROTOC = protoc
LIBS = -lprotobuf

# 目录定义
SRC_DIR = ../src
BUILD_DIR = ../build

# 源文件
PARSER_SOURCES = $(BUILD_DIR)/moparse_lib.cpp $(BUILD_DIR)/modb.pb.cc
PARSER_OBJECTS = $(BUILD_DIR)/moparse_lib.o $(BUILD_DIR)/modb.pb.o

# 测试目标
TEST_TARGETS = test_moparse test_verilog_syntax test_edge_cases test_reverse_generation test_filelist_features test_all

# 命令行测试
CLI_TEST_SCRIPT = test_cli.sh

.PHONY: test clean all run-all-tests

all: prepare-lib $(TEST_TARGETS)

# 准备库文件（从构建build目录复制moparse.cpp并移除main函数）
prepare-lib:
	@mkdir -p $(BUILD_DIR)
	@if [ ! -f $(BUILD_DIR)/moparse_lib.cpp ]; then \
		sed '/^int main/,$$d' $(SRC_DIR)/moparse.cpp > $(BUILD_DIR)/moparse_lib.cpp; \
	fi

# 生成Protocol Buffers代码（如果不存在）
$(BUILD_DIR)/modb.pb.cc $(BUILD_DIR)/modb.pb.h: $(SRC_DIR)/modb.proto
	@mkdir -p $(BUILD_DIR)
	cd $(SRC_DIR) && $(PROTOC) --proto_path=. --cpp_out=../build modb.proto

# 编译通用目标文件
$(BUILD_DIR)/%.o: $(BUILD_DIR)/%.cpp
	$(CXX) $(CXXFLAGS) -c $< -o $@

$(BUILD_DIR)/modb.pb.o: $(BUILD_DIR)/modb.pb.cc $(BUILD_DIR)/modb.pb.h
	$(CXX) $(CXXFLAGS) -c $(BUILD_DIR)/modb.pb.cc -o $(BUILD_DIR)/modb.pb.o

# 编译各个测试可执行文件
test_moparse: test_moparse.cpp $(PARSER_OBJECTS)
	$(CXX) $(CXXFLAGS) test_moparse.cpp $(PARSER_OBJECTS) -o test_moparse $(LIBS)

test_verilog_syntax: test_verilog_syntax.cpp $(PARSER_OBJECTS)
	$(CXX) $(CXXFLAGS) test_verilog_syntax.cpp $(PARSER_OBJECTS) -o test_verilog_syntax $(LIBS)

test_edge_cases: test_edge_cases.cpp $(PARSER_OBJECTS)
	$(CXX) $(CXXFLAGS) test_edge_cases.cpp $(PARSER_OBJECTS) -o test_edge_cases $(LIBS)

test_reverse_generation: test_reverse_generation.cpp $(PARSER_OBJECTS)
	$(CXX) $(CXXFLAGS) test_reverse_generation.cpp $(PARSER_OBJECTS) -o test_reverse_generation $(LIBS)

test_filelist_features: test_filelist_features.cpp $(PARSER_OBJECTS)
	$(CXX) $(CXXFLAGS) test_filelist_features.cpp $(PARSER_OBJECTS) -o test_filelist_features $(LIBS)

test_all: test_all.cpp
	$(CXX) $(CXXFLAGS) test_all.cpp -o test_all

# 运行所有测试
run-all-tests: $(TEST_TARGETS) $(CLI_TEST_SCRIPT)
	@echo "运行综合测试套件..."
	./test_all
	@echo "\n运行命令行测试..."
	bash $(CLI_TEST_SCRIPT)

# 运行单个测试
test: test_moparse $(CLI_TEST_SCRIPT)
	@echo "运行基本单元测试..."
	./test_moparse
	@echo "\n运行命令行测试..."
	bash $(CLI_TEST_SCRIPT)

# 运行语法测试
test-syntax: test_verilog_syntax
	@echo "运行Verilog语法测试..."
	./test_verilog_syntax

# 运行边界测试
test-edge: test_edge_cases
	@echo "运行边界情况测试..."
	./test_edge_cases

# 运行反向生成测试
test-reverse: test_reverse_generation
	@echo "运行反向生成测试..."
	./test_reverse_generation

# 运行文件列表测试
test-filelist: test_filelist_features
	@echo "运行文件列表测试..."
	./test_filelist_features

# 快速测试（只运行基本测试）
quick-test: test_moparse
	./test_moparse

# 清理
clean:
	rm -f *.o $(TEST_TARGETS)
	rm -f test_*.v test_*.modb verilog.list *.list
	rm -f $(BUILD_DIR)/modb.pb.cc $(BUILD_DIR)/modb.pb.h $(BUILD_DIR)/modb.pb.o $(BUILD_DIR)/moparse_lib.cpp $(BUILD_DIR)/moparse_lib.o
	rm -rf subdir1 subdir2

# 显示帮助
help:
	@echo "可用的make目标:"
	@echo "  all           - 编译所有测试"
	@echo "  test          - 运行基本测试"
	@echo "  run-all-tests - 运行所有测试套件"
	@echo "  test-syntax   - 运行Verilog语法测试"
	@echo "  test-edge     - 运行边界情况测试"
	@echo "  test-reverse  - 运行反向生成测试"
	@echo "  test-filelist - 运行文件列表测试"
	@echo "  quick-test    - 快速测试（仅基本功能）"
	@echo "  clean         - 清理编译文件"
	@echo "  help          - 显示此帮助信息"